using SimpleJSON;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class TotalNumHelper : NodeHelper
    {
        public enum CompareType
        {
            None,
            Zero,
            Equal,
            UnEqual,
            GreaterOrEqual,
            LessOrEqual,
            Greater,
            Less
        }

        [SelectRef(UIKey.ATTR_KEY)] public string numKey;
        [SelectRef(UIKey.ATTR_KEY)] public string numTotalKey;
        [SelectRef(UIKey.DATA_KEY)] public SelectKey numDataKey;
        [SelectRef(UIKey.DATA_KEY)] public SelectKey numTotalDataKey;
        public CompareType compareType = CompareType.None;
        public string format;
        public string successFormat;
        public string failFormat;
        public bool local;
        public bool online;
        public bool pro;
        public bool max;
        public bool attr = true;
        public BindDataType dataType = BindDataType.Object;
        private Text _Text;
        private TextMeshProUGUI _TextPro;

        private Text Text
        {
            get
            {
                if (_Text == null) _Text = GetComponent<Text>();
                return _Text;
            }
        }

        private TextMeshProUGUI TextPro
        {
            get
            {
                if (_TextPro == null) _TextPro = GetComponent<TextMeshProUGUI>();
                return _TextPro;
            }
        }

        protected override BindDataType bindDataType => dataType;

        protected override void BindData(JSONObject obj)
        {
            int num, numTotal;
            if (attr)
            {
                num = obj[numKey].AsInt;
                numTotal = obj[numTotalKey].AsInt;
            }
            else
            {
                num = numDataKey.Get(obj);
                numTotal = numTotalDataKey.Get(obj);
            }

            if (max && num > numTotal) num = numTotal;
            
            var tmp = new object[2] { num, numTotal };
            var _f = format;
            switch (compareType)
            {
                case CompareType.None:
                    break;
                case CompareType.Zero:
                    _f = num != 0 ? successFormat : failFormat;
                    break;
                case CompareType.Equal:
                    _f = num == numTotal ? successFormat : failFormat;
                    break;
                case CompareType.UnEqual:
                    _f = num != numTotal ? successFormat : failFormat;
                    break;
                case CompareType.Greater:
                    _f = num > numTotal ? successFormat : failFormat;
                    break;
                case CompareType.Less:
                    _f = num < numTotal ? successFormat : failFormat;
                    break;
                case CompareType.GreaterOrEqual:
                    _f = num >= numTotal ? successFormat : failFormat;
                    break;
                case CompareType.LessOrEqual:
                    _f = num <= numTotal ? successFormat : failFormat;
                    break;
            }

            SetText(string.Format(GetText(_f), tmp));
        }

        private string GetText(string s)
        {
            // Debug.LogErrorFormat("{0} {1} {2}", gameObject.name, gameObject.transform.parent.name ,s);
            if (online && !StringTools.IsEmpty(s))
            {
                var o = Localization.Instance.GetOnlineValue(s);
                s = o == "" ? s : o;
            }

            return s;
        }

        private void SetText(string t)
        {
            if (pro)
                TextPro.text = t;
            else
                Text.text = t;
        }

        private void SetColor(Color c)
        {
            if (pro)
                TextPro.color = c;
            else
                Text.color = c;
        }
    }
}